package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    @Override
    public List<TbBusinessTrackRecord> getList(Long id) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecords =  tbBusinessTrackRecordMapper.getList(id);
        List<TbBusinessTrackRecord> collect = tbBusinessTrackRecords.stream().map(item -> {
            String keyItems = item.getKeyItems();
            String[] split = keyItems.split(",");
            List<String> keys = new ArrayList<>();
            for (String s : split) {
                String key = tbBusinessTrackRecordMapper.getKeys(s);
                keys.add(key);
            }
            item.setKeys(keys);
            return item;
        }).collect(Collectors.toList());
        return collect;

    }

    @Override
    public void inertTrackRecord(BusinessTrackVo businessTrackVo) {
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();

        BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
        tbBusinessTrackRecordMapper.insertBusinessTrackRecord(tbBusinessTrackRecord);
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

    }
}
